import Axios from 'axios';
let commonIp = '' // 公用ip
let wxIp = ""; //线上环境ip

if (process.env.NODE_ENV === 'development') { //开发环境;
    // commonIp = 'http://192.168.3.5:50000'
    // wxIp = 'http://192.168.3.5:1120';
    // commonIp = 'https://oa.qizhkj.com:50000'
    commonIp = 'https://item.qizhkj.com:1020'; //鹤壁公司
    // commonIp = 'http://192.168.3.81:50000'; //鹤壁公司
    // commonIp = 'http://4064j91r33.zicp.vip'; // ui查看
    // 一次性
    // commonIp = 'http://192.168.3.81:50000'
    wxIp = 'https://item.qizhkj.com';
} else if (process.env.NODE_ENV === 'production') { // 打包环境
    commonIp = 'https://oa.qizhkj.com:50000'; //鹤壁公司
    // commonIp = 'http://192.168.3.81:50000'; //鹤壁公司
    // commonIp = 'http://4064j91r33.zicp.vip'; // ui查看
    // 一次性
    // commonIp = 'http://192.168.3.81:50000'
    wxIp = 'https://item.qizhkj.com';
}
let ip = commonIp + '/sys'; // 其他ip
let yxglIp = commonIp + '/mark' //  营销管理
let fysqIp = commonIp + '/apply' //费用申请
Axios.interceptors.request.use(
    config => {
        // console.log(sessionStorage.getItem("wxToken"));
        if (sessionStorage.getItem("wxToken")) {
            // console.log(sessionStorage.getItem("wxToken"));
            config.headers.access = sessionStorage.getItem("wxToken");
        }else{
            // console.log(sessionStorage.getItem("token"));
            config.headers.authentication = sessionStorage.getItem("token");
        }
        // console.log(sessionStorage.getItem("wxToken"));
        // console.log(config.headers);
        return config;
    },
    error => {
        // console.log("===发送请求拦截器报错===")
        // console.log(error);
        // console.log("===end===");
        Promise.reject(error);
    }
);

Axios.interceptors.response.use(
    response => {
        //拦截响应，做统一处理 
        switch (response.data.code) {
            case 10021:
                // window.open('/login', '_self');
                break;
            case 10019:
                sessionStorage.removeItem('token');
                sessionStorage.removeItem('wxToken');
                // window.open('/login', '_self');
                break;
            default:
                break;

        }
        return response
    },
    //接口错误状态处理，也就是说无响应时的处理
    error => {
        // window.open('/login', '_self');
        return Promise.reject(error.response.status) // 返回接口返回的错误信息
    })

//封装请求方法
const http = (
    url,  //地址
    method, //请求方式
    args = {} //参数
) => {
    const xhrArgsName = (method === "get" || method === 'delete') ? "params" : "data"; //根据请求方式,判断携带的参数类型;
    return new Promise((resolve, reject) => {
        Axios({
            url,
            method,
            [xhrArgsName]: args
        }).then(response => {
            resolve(response.data);
        }).catch(error => {
            reject(error);
        })
    })
};

export {
    http,
    ip,
    yxglIp,
    fysqIp,
    commonIp,
    wxIp
};